#include <iostream>
#include <map>

using namespace std;

struct ListNode {
	int val;
	struct ListNode *next;
	ListNode(int x) :
		val(x), next(NULL) {
	}
};


ListNode* EntryNodeOfLoop(ListNode* pHead)
{
	if (pHead == NULL) return NULL;
	map<ListNode*, int> nodeMap;
	ListNode *p = pHead;
	while (p != NULL)
	{
		++nodeMap[p];
		if (nodeMap[p] == 2)
			return p;
		p = p->next;
	}
	return NULL;
}